package com.topvision.prlibrary;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.storage.StorageManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.kernal.lisence.Common;
import com.kernal.lisence.DeviceFP;
import com.kernal.plateid.PlateCfgParameter;
import com.kernal.plateid.PlateIDAPI;
import com.kernal.plateid.PlateRecognitionParameter;
import com.kernal.plateid.TH_PlateIDCfg;
import com.kernal.plateid.TH_PlateIDResult;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RecogService extends Service {
    public static final String TAG = "RecogService";
    private static Common common = new Common();
    public RecogService.MyBinder binder;
    private PlateIDAPI plateIDAPI;
    private ConfigArgument configArg;
    private int iTH_InitPlateIDSDK = -1;
    private int nRet = -1;
    private int nResultNum = 0;
    public static boolean initializeType = false;
    public static boolean recogModel = true;
    private byte[] data = null;

    public RecogService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return this.binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.iTH_InitPlateIDSDK = -10003;
        this.binder = new RecogService.MyBinder();
        this.configArg = new ConfigArgument();
        TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
        DeviceFP deviceFP = new DeviceFP();
        deviceFP.deviceid = "DeviceIdIsNull";
        this.plateIDAPI = new PlateIDAPI();
        TH_PlateIDCfg c_Config1 = new TH_PlateIDCfg();
        if(initializeType && recogModel) {
            c_Config1.bMovingImage = 2;
        }
        this.iTH_InitPlateIDSDK = this.plateIDAPI.TH_InitPlateIDSDK(c_Config1, telephonyManager, deviceFP);
        Log.v("RecogService", "初始化返回值:=" + this.iTH_InitPlateIDSDK);
    }

    private synchronized String[] recogPlate(byte[] picByte, String pic, int width, int height, String userData, TH_PlateIDCfg plateIDCfg) {
        int[] nResultNums = new int[]{10};
        int[] nRets = new int[]{-1};
        TH_PlateIDResult plateidresult = new TH_PlateIDResult();
        TH_PlateIDResult[] plateidresults;
        if(picByte != null && picByte.length > 0) {
            plateidresults = this.plateIDAPI.TH_RecogImageByte(picByte, width, height, plateidresult, nResultNums, plateIDCfg.left, plateIDCfg.top, plateIDCfg.right, plateIDCfg.bottom, nRets, plateIDCfg.bRotate);
        } else {
            plateidresults = this.plateIDAPI.TH_RecogImage(pic, width, height, plateidresult, nResultNums, 0, 0, 0, 0, nRets);
        }

        this.nRet = nRets[0];
        this.nResultNum = nResultNums[0];
        String[] tempFieldvalue = new String[15];
        if(nRets[0] != 0) {
            tempFieldvalue[14] = userData;
        } else {
            tempFieldvalue[14] = userData;

            for(int i = 0; i < nResultNums[0]; ++i) {
                if(plateidresults != null && plateidresults[i] != null) {
                    if(i == 0) {
                        tempFieldvalue[0] = plateidresults[i].license;
                        tempFieldvalue[1] = plateidresults[i].color;
                        tempFieldvalue[2] = this.int2string(plateidresults[i].nColor);
                        tempFieldvalue[3] = this.int2string(plateidresults[i].nType);
                        tempFieldvalue[4] = this.int2string(plateidresults[i].nConfidence);
                        tempFieldvalue[5] = this.int2string(plateidresults[i].nBright);
                        tempFieldvalue[6] = this.int2string(plateidresults[i].nDirection);
                        tempFieldvalue[7] = this.int2string(plateidresults[i].left);
                        tempFieldvalue[8] = this.int2string(plateidresults[i].top);
                        tempFieldvalue[9] = this.int2string(plateidresults[i].right);
                        tempFieldvalue[10] = this.int2string(plateidresults[i].bottom);
                        tempFieldvalue[11] = this.int2string(plateidresults[i].nTime);
                        tempFieldvalue[12] = this.int2string(plateidresults[i].nCarBright);
                        tempFieldvalue[13] = this.int2string(plateidresults[i].nCarColor);
                        tempFieldvalue[14] = userData;
                        this.data = plateidresults[i].pbyBits;
                    } else {
                        tempFieldvalue[0] = tempFieldvalue[0] + ";" + plateidresults[i].license;
                        tempFieldvalue[1] = tempFieldvalue[1] + ";" + plateidresults[i].color;
                        tempFieldvalue[2] = tempFieldvalue[2] + ";" + this.int2string(plateidresults[i].nColor);
                        tempFieldvalue[3] = tempFieldvalue[3] + ";" + this.int2string(plateidresults[i].nType);
                        tempFieldvalue[4] = tempFieldvalue[4] + ";" + this.int2string(plateidresults[i].nConfidence);
                        tempFieldvalue[5] = tempFieldvalue[5] + ";" + this.int2string(plateidresults[i].nBright);
                        tempFieldvalue[6] = tempFieldvalue[6] + ";" + this.int2string(plateidresults[i].nDirection);
                        tempFieldvalue[7] = tempFieldvalue[7] + ";" + this.int2string(plateidresults[i].left);
                        tempFieldvalue[8] = tempFieldvalue[8] + ";" + this.int2string(plateidresults[i].top);
                        tempFieldvalue[9] = tempFieldvalue[9] + ";" + this.int2string(plateidresults[i].right);
                        tempFieldvalue[10] = tempFieldvalue[10] + ";" + this.int2string(plateidresults[i].bottom);
                        tempFieldvalue[11] = tempFieldvalue[11] + ";" + this.int2string(plateidresults[i].nTime);
                        tempFieldvalue[12] = tempFieldvalue[12] + ";" + this.int2string(plateidresults[i].nCarBright);
                        tempFieldvalue[13] = tempFieldvalue[13] + ";" + this.int2string(plateidresults[i].nCarColor);
                    }
                }
            }
        }

        plateidresults = null;
        return tempFieldvalue;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(this.iTH_InitPlateIDSDK == 0) {
            int iTH_UninitPlateIDSDK = this.plateIDAPI.TH_UninitPlateIDSDK();
            Log.i("RecogService", "iTH_UninitPlateIDSDK=" + iTH_UninitPlateIDSDK);
        }

        this.data = null;
    }

    public String readInitFileString(String filePathString) {
        String SysCertVersion = "wtversion5_5";
        String deviceidString = "";
        File dateInitFile = new File(filePathString);
        if(dateInitFile.exists()) {
            try {
                BufferedReader e = new BufferedReader(new FileReader(dateInitFile));
                deviceidString = e.readLine();
                e.close();
                deviceidString = common.getSrcPassword(deviceidString, SysCertVersion);
            } catch (FileNotFoundException var6) {
                deviceidString = "";
                var6.printStackTrace();
            } catch (IOException var7) {
                deviceidString = "";
                var7.printStackTrace();
            } catch (Exception var8) {
                deviceidString = "";
                var8.printStackTrace();
            }
        }

        return deviceidString;
    }

    private String int2string(int i) {
        String str = "";

        try {
            str = String.valueOf(i);
        } catch (Exception var4) {
            ;
        }

        return str;
    }

    public String getSDPath(Context mContext, boolean is_removale) {
        String path = "";
        String temppath = "";
        String lastPath = "";
        StorageManager mStorageManager = (StorageManager)mContext.getSystemService(STORAGE_SERVICE);
        Class storageVolumeClazz = null;

        try {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
            Method e = mStorageManager.getClass().getMethod("getVolumeList", new Class[0]);
            Method getPath = storageVolumeClazz.getMethod("getPath", new Class[0]);
            Method isRemovable = storageVolumeClazz.getMethod("isRemovable", new Class[0]);
            Object result = e.invoke(mStorageManager, new Object[0]);
            int length = Array.getLength(result);

            for(int i = 0; i < length; ++i) {
                Object storageVolumeElement = Array.get(result, i);
                temppath = (String)getPath.invoke(storageVolumeElement, new Object[0]);
                boolean removable = ((Boolean)isRemovable.invoke(storageVolumeElement, new Object[0])).booleanValue();
                if(is_removale == removable) {
                    if(path != null && !path.equals("")) {
                        if(!temppath.equals(lastPath)) {
                            path = path + "#" + temppath;
                            lastPath = temppath;
                        }
                    } else if(path != null && path.equals("")) {
                        path = temppath;
                        lastPath = temppath;
                    }
                }
            }
        } catch (ClassNotFoundException var16) {
            var16.printStackTrace();
        } catch (InvocationTargetException var17) {
            var17.printStackTrace();
        } catch (NoSuchMethodException var18) {
            var18.printStackTrace();
        } catch (IllegalAccessException var19) {
            var19.printStackTrace();
        }

        return path;
    }

    public class MyBinder extends Binder {
        public MyBinder() {
        }

        public void setRecogArgu(PlateCfgParameter cfgparameter, int imageformat, int bVertFlip, int bDwordAligned) {
            RecogService.this.nRet = -10000;
            if(RecogService.this.iTH_InitPlateIDSDK != 0) {
                RecogService.this.nRet = -10001;
            } else {
                RecogService.this.plateIDAPI.TH_SetImageFormat(imageformat, bVertFlip, bDwordAligned);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.individual);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.tworowyellow);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.armpolice);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.tworowarmy);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.tractor);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.onlytworowyellow);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.embassy);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.onlylocation);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.armpolice2);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.newEnergy);
                RecogService.this.plateIDAPI.TH_SetEnabledPlateFormat(cfgparameter.consulate);
                RecogService.this.plateIDAPI.TH_SetRecogThreshold(cfgparameter.nPlateLocate_Th, cfgparameter.nOCR_Th);
                RecogService.this.plateIDAPI.TH_SetAutoSlopeRectifyMode(cfgparameter.bIsAutoSlope, cfgparameter.nSlopeDetectRange);
                RecogService.this.plateIDAPI.TH_SetProvinceOrder(cfgparameter.szProvince);
                if(cfgparameter.nContrast != 0) {
                    RecogService.this.plateIDAPI.TH_SetContrast(cfgparameter.nContrast);
                }
                RecogService.this.plateIDAPI.TH_SetDayNightMode(cfgparameter.bIsNight);
                RecogService.this.nRet = 0;
            }
        }

        public String[] doRecogDetail(PlateRecognitionParameter prp) {
            Log.e(TAG, "doRecogDetail: ");
            return RecogService.this.recogPlate(prp.picByte, prp.pic, prp.width, prp.height, prp.userData, prp.plateIDCfg);
        }

        public String[] doRecogDetail(byte[] picByte, String pic, int width, int height, String userData) {
            PlateRecognitionParameter prp = new PlateRecognitionParameter();
            prp.picByte = picByte;
            prp.pic = pic;
            prp.width = width;
            prp.height = height;
            prp.userData = userData;
            return this.doRecogDetail(prp);
        }

        public int getInitPlateIDSDK() {
            return RecogService.this.iTH_InitPlateIDSDK;
        }

        public int getnRet() {
            return RecogService.this.nRet;
        }

        public int getnResultNums() {
            return RecogService.this.nResultNum;
        }

        public int UninitPlateIDSDK() {
            int iTH_UninitPlateIDSDK = -1;
            if(RecogService.this.iTH_InitPlateIDSDK == 0) {
                iTH_UninitPlateIDSDK = RecogService.this.plateIDAPI.TH_UninitPlateIDSDK();
                Log.v("RecogService", "iTH_UninitPlateIDSDK=" + iTH_UninitPlateIDSDK);
            }

            return iTH_UninitPlateIDSDK;
        }

        public byte[] getRecogData() {
            return RecogService.this.data;
        }
    }
}
